#include "transfer.h"
#include "ui_transfer.h"

#include <common/downloadlayout.h>
#include <common/logininfoinstance.h>
#include <common/uploadlayout.h>

#include <QFile>

transfer::transfer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::transfer)
{
    ui->setupUi(this);

    UploadLayout* uploadLayout = UploadLayout::getInstance();
    ui->upload_scrollArea->setLayout(new QVBoxLayout(ui->upload_scrollArea));
    uploadLayout->setUploadLayout(ui->upload_scrollArea);

    DownloadLayout* downloadLayout = DownloadLayout::getInstance();
    ui->download_scrollArea->setLayout(new QVBoxLayout(ui->download_scrollArea));
    downloadLayout->setDownloadLayout(ui->download_scrollArea);

    // 切换tab页
    connect(ui->tabWidget, &QTabWidget::currentChanged, [=](int index)
    {
        if(index == 0) //上传
        {
            emit currentTabSignal("正在上传");
        }
        else if(index == 1)//下载
        {
             emit currentTabSignal("正在下载");
        }
        else //传输记录
        {
            emit currentTabSignal("传输记录");
            dispayDataRecord(); //显示数据传输记录
        }
    });
}

transfer::~transfer()
{
    delete ui;
}

void transfer::dispayDataRecord(QString path)
{
    //获取登陆信息实例
    LoginInfoInstance *login = LoginInfoInstance::getInstance(); //获取单例
    QString filename = path + login->getUser();
    QFile file(filename);

    if(false == file.open(QIODevice::ReadOnly))
    {
        cout << "file.open(QIODevice::ReadOnly) err";
        return;
    }

    QByteArray array = file.readAll();
#ifdef _WIN32 //如果是windows平台
    //fromLocal8Bit(), 本地字符集转换为utf-8
    ui->record_msg->setText( QString::fromLocal8Bit(array) );
#else //其它平台
    ui->record_msg->setText( array );
#endif

    file.close();
}
